//
//  StatusReweteedView.swift
//  SHWeibo2.0
//
//  Created by 黄礼潮 on 16/2/28.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit
//被转发微博

class StatusReweteedView: UIView {
    
    //被转发微博模型
    var viewModel: StatusViewModel?{
    
        didSet{
            reweteedLabel.text = viewModel?.reweteedText
          //判断是否有转发微博
            if viewModel?.status.retweeted_status == nil {
             reweteedLabel.snp_updateConstraints(closure: { (make) -> Void in
                    make.top.equalTo(self)
                })
                pictureView.snp_updateConstraints(closure: { (make) -> Void in
                    make.size.equalTo(CGSizeZero)
                    make.top.equalTo(reweteedLabel.snp_bottom)
                    make.bottom.equalTo(self)
                })
                
                return
            
            }
            
            //更新配图的大小
            var offset:CGFloat = 0
            if viewModel?.status.retweeted_status?.pic_urls?.count > 0{
                 offset = CZCellViewLayout.margin
            }
            //更新配图的大小
            reweteedLabel.snp_updateConstraints { (make) -> Void in
                make.top.equalTo(self).offset(offset)
            }
            pictureView.snp_updateConstraints(closure:  {(make) -> Void in
                make.top.equalTo(reweteedLabel.snp_bottom)
                make.size.equalTo(viewModel!.reweteedPictureViewSize)
                make.bottom.equalTo(self).offset(-offset)
            })
            //设置图像 传递数据
            pictureView.pic_urls = viewModel?.status.retweeted_status?.pic_urls


       }

        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupPictuerUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //添加控件
    private lazy var reweteedLabel:UILabel = UILabel(cztitle:"@作者:微博", fontSize:14)
    //添加配图
    private lazy var pictureView:StatusPictuerView = StatusPictuerView()
}


extension StatusReweteedView{
    
    private func setupPictuerUI(){
        
        backgroundColor = UIColor.cz_colorWithHex(0xF5F5F5)
        //添加视图
       addSubview(reweteedLabel)
       addSubview(pictureView)
        //自动布局
        let layout = CZCellViewLayout
        reweteedLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self).offset(layout.margin)
            make.right.equalTo(self).offset(-layout.margin)
            make.top.equalTo(self).offset(layout.margin)
            
        }
        
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(reweteedLabel.snp_bottom).offset(layout.margin)
            make.left.equalTo(reweteedLabel)
            make.size.equalTo(CGSize(width: 351, height: 250))
            make.bottom.equalTo(self).offset(-layout.margin)
        }
        
    }
}